home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 445_01 / cdntmr11 / cdntimer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-15  |  7.8 KB  |  315 lines

  1. /****************************** BOARD GAME TIMER****************************
  2. Full-featured "turn timer"      Digital displays of time
  3. for board games.  
  4. Also usable as cooking                            
  5. darkroom timer, etc.                 
  6.                 Low-time warning (optional)
  7.                 Flashing "tick" indicator (optional)
  8.     Beep on reset
  9.  
  10. M\Cooper
  11. 3425 Chestnut Ridge Rd.
  12. Grantsville, MD 21536-9801
  13. Email:  thegrendel@aol.com
  14. ****************************************************************************/
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include <conio.h>
  20. #include <stdlib.h>
  21. #include <graphics.h>
  22. #include <dos.h>
  23. #include "oscr.hpp"
  24. #include "bt.hpp"
  25.  
  26. void main()
  27. {
  28.    randomize();
  29.    opening_screen();
  30.    play();
  31. }
  32.  
  33. void CountdownTimer::clock_on()
  34. {
  35.    time_t prev_sec;
  36.    int ch;
  37.  
  38.       text_color = LIGHTRED;
  39.  
  40.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  41.       settextjustify( LEFT_TEXT, TOP_TEXT );
  42.  
  43.       startn_t = time ( NULL );  //Click on stopwatch.
  44.       setcolor( BKGRND_COLOR );
  45.       outtextxy( NAME_POS, 100, "Press a key to reset timer" );
  46.       setcolor( LIGHTBLUE );
  47.       outtextxy( NAME_POS, 100, "Press a key to stop timer" );
  48.  
  49.  
  50.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  51.       display_time();  //Otherwise initial time not displayed...
  52.  
  53.       while( !( ch = kbhit() ) )
  54.      {
  55.      prev_sec = seconds;
  56.      interval_t = time( NULL ) - start_t;
  57.      running_t = total_seconds - interval_t;
  58.      convert( running_t );
  59.  
  60.      if( seconds - prev_sec )
  61.         {
  62.         display_time();
  63.  
  64.         if( !seconds )
  65.            if( minutes == warning )
  66.           if( !hours )
  67.              if( time_warning_flag )
  68.             blatt();
  69.  
  70.         if( visual_ticking_flag ) // Show blinking box ticks?
  71.            {
  72.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  73.            setcolor ( random ( COLORS ) );
  74.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  75.            bar( X_C - RADIUS, Y_C - RADIUS,
  76.             X_C + RADIUS, Y_C + RADIUS );
  77.            }
  78.         }
  79.  
  80.      if( timeout() )
  81.         {
  82.         exit_();
  83.         reset_timer();
  84.  
  85.         settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  86.         display_time();
  87.         return;  /*Move counter increments*/
  88.         }
  89.  
  90.      }
  91.  
  92.       ch = getch();
  93.       if( ch == ESC )
  94.      exit__();  // Quit.
  95.  
  96.       beep1();  //<--------
  97.       running_flag = OFF; // Reset each time.
  98.       setcolor( BKGRND_COLOR );
  99.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  100.       outtextxy( NAME_POS, 100, "Press a key to stop timer" );
  101.       setcolor( CYAN );
  102.       outtextxy( NAME_POS, 100, "Press a key to reset timer" );
  103.       ch = getch();
  104.       if( ch == ESC )
  105.      exit__(); // Quit.
  106.  
  107.       reset_timer(); 
  108.  
  109.       return;
  110.  
  111. }
  112.  
  113. void CountdownTimer::display_moves()
  114. {
  115.    char buf[ 5 ];
  116.    static char ebuf[ 5 ];
  117.  
  118.       if( moves > 1 )
  119.      {
  120.      setcolor( WHITE );
  121.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  122.      settextjustify( CENTER_TEXT, TOP_TEXT );
  123.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  124.      }
  125.  
  126.       sprintf( buf, "%003d", moves );
  127.       sprintf( ebuf, buf );
  128.       setcolor( GREEN );
  129.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  130.       settextjustify( CENTER_TEXT, TOP_TEXT );
  131.       outtextxy( MOVES_X, MOVES_Y, buf );
  132.       
  133.       return;
  134. }
  135.  
  136. void graphics_setup( int background_color )
  137. {
  138.    int grdriver = VGA,
  139.        grmode = VGAHI;
  140.  
  141.        registerfarbgidriver( EGAVGA_driver_far );
  142.        registerfarbgifont( gothic_font_far );
  143.        registerfarbgifont( triplex_font_far );
  144.        initgraph( &grdriver, &grmode, "" );
  145.        setbkcolor( background_color );
  146.  
  147. }
  148.  
  149. void exit__()
  150. {
  151.       closegraph();
  152.       exit( QUIT );
  153. }
  154.  
  155.     /***************Routine to erase old numbers*************/
  156. void CountdownTimer::erase_numbers()
  157. {
  158.       setcolor ( WHITE ); 
  159.  
  160.       if( seconds == 59 )
  161.      outtextxy( BLK_TIME, Y_TIMEPOS, line_clear );
  162.       else
  163.      if( seconds == 9 || seconds == 19 || seconds == 29
  164.          || seconds == 39 || seconds == 49 )
  165.         outtextxy( BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  166.                line_clear + 6 );
  167.       else
  168.          outtextxy( BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  169.             line_clear + 7 ); 
  170.  
  171.      return;
  172.  
  173. }
  174.  
  175.  
  176. void play()
  177. {
  178.    int hrs,
  179.        min;
  180.    char inputstr[ MAXLEN ],
  181.     inp;
  182.  
  183.       clrscr();
  184.  
  185.       textcolor ( RED );
  186.       cprintf( "\n                                   MINUTES: " );
  187.       gets( inputstr );
  188.       min = atoi( inputstr );
  189.       CountdownTimer t1( min );
  190.  
  191.       textcolor( CYAN );
  192.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  193.       inp = getche();
  194.       if( inp == 'y' || inp == 'Y' )
  195.      t1.visual_ticking_flag = ON;
  196.       else
  197.      t1.visual_ticking_flag = OFF;
  198.  
  199.       cprintf( "\n                                                      Enable time warning? " );
  200.       inp = getche();
  201.       if( inp == 'y' || inp == 'Y' )
  202.      {
  203.      t1.time_warning_flag =  ON;
  204.      cprintf( "                                                          At how many minutes? " );
  205.      gets( inputstr );
  206.      t1.warning = atoi( inputstr );
  207.      }
  208.       else
  209.      t1.time_warning_flag = OFF;
  210.  
  211.       textcolor( YELLOW | BLINK );
  212.       _setcursortype( _NOCURSOR );
  213.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  214.       cprintf( "                             PRESS A KEY TO BEGIN" );
  215.       while ( !getch() );
  216.  
  217.       graphics_setup( WHITE );
  218.  
  219.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  220.       settextstyle( GOTHIC_FONT, HORIZ_DIR, 4 );
  221.       setcolor( LIGHTMAGENTA );
  222.       outtextxy( TOPX, TOPY - 30, title_msg );  /*******************/
  223.  
  224.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  225.       setcolor( LIGHTGREEN );
  226.       outtextxy( TOPX, TOPY + 20, esc_msg );
  227.  
  228.       t1.initialize_clock();
  229.       t1.moves++;
  230.       t1.display_moves();
  231.       t1.clock_on();
  232.  
  233.       while ( PLAY )  //Forever, until keypress
  234.      {
  235.       beep1();
  236.      t1.moves++;  
  237.      t1.display_moves();
  238.      t1.clock_on();
  239.      }
  240.  
  241.  
  242. } // End play()
  243.  
  244.  
  245. void opening_screen()
  246. {
  247.    char topline[] = "Countdown Timer",
  248.     by_line[] = "by",
  249.     name_line[] = "M\\Cooper",
  250.     endline[] = "PRESS A KEY";
  251.  
  252.       graphics_setup( LIGHTCYAN );
  253.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  254.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  255.       setcolor( LIGHTRED );
  256.       outtextxy( TOPX, TOPY, topline );
  257.  
  258.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  259.       setcolor( BLUE );
  260.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  261.  
  262.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  263.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  264.  
  265.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  266.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  267. //    pieslice( PIE2_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  268.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  269. //    circle( PIE2_X, PIE_Y, CIRC_RAD );
  270.       setlinestyle( SOLID_LINE, 0xFFFF, THICK_WIDTH );
  271.       setcolor( LIGHTRED );
  272.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  273. //    line( LINE2_X, LINE_Y1, LINE2_X, LINE2_Y2 );
  274.       setfillstyle( PIE_PATTERN, WHITE );
  275.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  276. //    bar( B2_LEFT, B2_TOP, B2_RIGHT, B_BOTTOM );
  277.  
  278.  
  279.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  280.       setcolor( BLUE );
  281.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  282.  
  283.       sleep( DELAY );
  284.  
  285.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  286.       setcolor( RED );
  287.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  288.  
  289.       getch();
  290.       closegraph();
  291.  
  292.       return;
  293. }
  294.  
  295.  
  296. void CountdownTimer::erase()
  297. {
  298.       display_time();
  299.       setcolor( WHITE );
  300.       outtextxy( BLK_TIME, Y_TIMEPOS, line_clear ); /* Erase time display */
  301.  
  302. }
  303.  
  304. void CountdownTimer::reset_timer()
  305. {
  306.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  307.       erase();
  308.       convert ( total_seconds_mem ); /***RESET TIME*****/
  309.       initialize_clock();
  310.       beep1();
  311.  
  312.       return;
  313.  
  314. }
  315.